package work.xiaohong.jijin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.xiaohong.jijin.entity.Zixuan;
import work.xiaohong.jijin.service.IZixuanService;
import work.xiaohong.model.Result;
import work.xiaohong.model.StatusCode;

import java.util.Arrays;
import java.util.List;

/**
 *  前端控制器
 * @author xiaohong
 * @since 2021-11-20 17:46:23
 */
@Tag(name = "Zixuan接口文档")
@RestController
@RequestMapping("/zixuan")
public class ZixuanController {

    @Autowired
    private IZixuanService zixuanService;


    /***
    * 条件分页查询
    * @param zixuan
    * @param page
    * @param size
    * @return
    */
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
        @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
        @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "Zixuan对象",required = true) Zixuan zixuan, @PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        Page<Zixuan> pageInfo = zixuanService.page(new Page<Zixuan>(page,size),new QueryWrapper<Zixuan>(zixuan));
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }


    /***
    * 分页查询
    * @param page:当前页
    * @param size:每页显示多少条
    * @return
    */
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
        @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
        @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        //调用ZixuanService实现分页查询Zixuan
        Page<Zixuan> pageInfo = zixuanService.page(new Page<>(page,size));
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
    * 条件查询
    * @param zixuan
    * @return
    */
    @Operation(summary = "条件查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<Zixuan>> findList(@RequestBody(required = false) @Parameter(name = "Zixuan对象",required = true) Zixuan zixuan){
        List<Zixuan> list = zixuanService.list(new QueryWrapper<Zixuan>(zixuan));
        return new Result<List<Zixuan>>(true,StatusCode.OK.getCode(),"查询成功",list);
    }


    /***
    * 根据code删除zixuan数据
    * @param code
    * @return
    */
    @Operation(summary = "根据code删除Zixuan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{code}" )
    public Result delete(@PathVariable(value="code") String code){
        zixuanService.removeById(code);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }



    /***
    * 根据code批量删除zixuan数据
    * @param ids
    * @return
    */
    @Operation(summary = "根据code批量删除Zixuan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @DeleteMapping("/ids")
    public Result deleteByIds(@RequestBody @Parameter(name = "需要删除的Zixuan对象code集合",required = true) List<String> ids){
        zixuanService.removeByIds(ids);
        return new Result(true,StatusCode.OK.getCode(),"删除成功");
    }


    /***
    * 根据ID修改Zixuan数据
    * @param zixuan
    * @return
    */
    @Operation(summary = "根据ID修改Zixuan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result updateById(@RequestBody @Parameter(name = "Zixuan对象",required = true) Zixuan zixuan){
        zixuanService.updateById(zixuan);
        return new Result(true,StatusCode.OK.getCode(),"修改成功");
    }


     /***
     * 新增Zixuan数据
     * @param zixuan
     * @return
     */
     @Operation(summary = "添加Zixuan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @PostMapping
     public Result add(@RequestBody  @Parameter(name = "Zixuan对象",required = true) Zixuan zixuan){
         if (zixuanService.getOne(new QueryWrapper<>(zixuan)) != null){
             return new Result(true,StatusCode.ERROR.getCode(),"重复添加！");
         }
         zixuanService.save(zixuan);
         return new Result(true,StatusCode.OK.getCode(),"添加成功");
     }

     /***
     * 根据ID查询Zixuan数据
     * @param id
     * @return
     */
      @Operation(summary = "根据ID查询Zixuan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
      @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
     @GetMapping("/{id}")
     public Result<Zixuan> findById(@PathVariable(value="id") Integer id){
        Zixuan zixuan = zixuanService.getById(id);
        return new Result<Zixuan>(true,StatusCode.OK.getCode(),"查询成功",zixuan);
     }

     /***
     * 根据Id批量查询Zixuan数据
     * @param ids
     * @return
     */
     @Operation(summary = "根据Id批量查询Zixuan数据",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @PostMapping("/ids")
     public Result<List<Zixuan>> findByIds(@RequestBody @Parameter(name = "需要查询的Zixuan对象id集合",required = true) List<String> ids){
        List<Zixuan> zixuanList = zixuanService.listByIds(ids);
        return new Result<List<Zixuan>>(true,StatusCode.OK.getCode(),"查询成功",zixuanList);
     }

     /***
     * 查询Zixuan全部数据
     * @return
     */
     @Operation(summary = "查询所有Zixuan",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
     @GetMapping
     public Result<List<Zixuan>> findAll(){
        List<Zixuan> list = zixuanService.list();
        return new Result<List<Zixuan>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
     }


}
